﻿package cn.jingsheng.backup.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 系统配置信息加载
 * 
 * @author 李晓丹
 * 
 */
public class SystemConfig {
	/**
	 * 序列化标志
	 */
	private static final long serialVersionUID = 5491099009156054570L;
	// 日志记录
	private static Log log = LogFactory.getLog(SystemConfig.class);
	// 默认配置文件名
	public static final String FILE_NAME = "system.config";
	// 配置信息
	private static Properties props = new Properties();

	/**
	 * 读取配置文件信息
	 * 
	 * @param key
	 * @return
	 * @throws IOException
	 */
	public static String getProperties(String key) {
		if (props.size() == 0) {
			try {
				InputStream inStream = SystemConfig.class.getClassLoader().getResourceAsStream(FILE_NAME);
				props.load(inStream);
			} catch (FileNotFoundException e) {
				log.error(e.getMessage(), e);
				throw new RuntimeException(e);
			} catch (IOException e) {
				log.error(e.getMessage(), e);
				throw new RuntimeException(e);
			}
		}
		return props.getProperty(key);
	}

	/**
	 * 读取配置文件信息,提供默认值操作
	 * 
	 * @param key
	 * @param defaultvalue
	 * @return
	 * @throws IOException
	 */
	public static String getProperties(String key, String defaultvalue) {
		String value = getProperties(key);
		if (value == null) {
			return defaultvalue;
		}
		return value;
	}

}
